package com.shenmazong.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shenmazong.dto.TbCompany;
import com.shenmazong.mapper.TbCompanyMapper;
import com.shenmazong.utils.AuthCodeUtil;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author 阿瑞
 * @version 1.0
 * @description TODO
 * @class DayNumReceive ???
 * @date 2021/9/27 8:58
 */
@Component
public class DayNumReceive {

    @Autowired
    private AmqpTemplate amqpTemplate;
    private ObjectMapper objectMapper=new ObjectMapper();
    @RabbitListener(queues = DayNumConfig.NUM_DEAD_QUEUE)
    public void dayNumReceive(String msg) throws JsonProcessingException {
        TbCompany tbCompany = objectMapper.readValue(msg, TbCompany.class);
        Integer dayNum = tbCompany.getDayNum();
        if(dayNum!=null&&dayNum<=1){
            if (!StringUtils.isEmpty(tbCompany.getCompanyTel())){
                AuthCodeUtil.sendMsgToPhone("您的提供的渠道还有"+dayNum+"天有效期",tbCompany.getCompanyTel());
            }
        }else {
            amqpTemplate.convertAndSend(DayNumConfig.NUM_EXCHANGE,DayNumConfig.NUM_DEAD_KEY,msg);
        }
    }
}
